package br.com.FII.validation;

import java.text.MessageFormat;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import br.com.FII.anotation.IntervaloString;

public class IntervaloValidation implements ConstraintValidator<IntervaloString, String> {

	private int valorMinimo = 0;

	private int valorMaximo;

	@Override
	public void initialize( IntervaloString intervalo ) {
		valorMinimo = intervalo.minimumValue();
		valorMaximo = intervalo.maximumValue();
	}

	@Override
	public boolean isValid( String valorRecebido, ConstraintValidatorContext contexto ) {
		if ( valorRecebido.length() < valorMinimo || valorRecebido.length() > valorMaximo ) {
			String template = contexto.getDefaultConstraintMessageTemplate();
			String message = MessageFormat.format( template, valorMinimo, valorMaximo );
			contexto.buildConstraintViolationWithTemplate( message ).addConstraintViolation().disableDefaultConstraintViolation();
			return false;
		}
		return true;
	}
}
